package com.up.upzl.biz.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.up.upzl.biz.bean.SysAccountInSession;
import com.up.upzl.biz.common.Constant;
import com.up.upzl.biz.domain.Article;
import com.up.upzl.biz.domain.ArticleCategory;
import com.up.upzl.biz.domain.SysAccount;
import com.up.upzl.biz.domain.Article.AddSource;
import com.up.upzl.biz.domain.Article.Auditing;
import com.up.upzl.biz.domain.Article.Label;
import com.up.upzl.biz.service.ArticleCategoryService;
import com.up.upzl.biz.service.ArticleService;
import com.up.upzl.biz.service.SysAccountService;


@Controller
@RequestMapping("article")
public class ArticleController {
	@Autowired
	private ArticleService articleService;
	
	@Autowired
	private ArticleCategoryService articleCategoryService;
	
	@Autowired
	private SysAccountService sysAccountService;

	@GetMapping("list")
	public String List(Long oneCatId, Long catId, AddSource addSource,Auditing auditing, String title,  Date beginTime, Date endTime, Pageable page, Model model) {
		if(catId != null) {
			List< Order> orders=new ArrayList< Order>();
			orders.add( new Order(Direction. DESC, "sort"));
			orders.add( new Order(Direction. DESC, "createTime"));
			page = new PageRequest(page.getPageNumber(), page.getPageSize(), new Sort(orders));
		}else {
			page = new PageRequest(page.getPageNumber(), page.getPageSize(), new Sort(Direction.DESC, "createTime"));
		}
		Page<Article> list = null;
		list = articleService.findByCondition(oneCatId, catId, addSource,auditing, title, beginTime, endTime, page);
		model.addAttribute("pageList", list);
		
		List<ArticleCategory> oneCategory = articleCategoryService.findByParentId(null);
		model.addAttribute("oneCategory", oneCategory);
		
		return "article/list";
	}

	/**
	 * 跳转到添加页面
	 * 
	 * @param model
	 * @return
	 */
	@GetMapping("addPage")
	public String add(Model model) {
		
		List<ArticleCategory> oneCategory = articleCategoryService.findByParentId(null);
		model.addAttribute("oneCategory", oneCategory);
		
		Label[] labelEnum = Label.values();
		model.addAttribute("label", labelEnum);
		
		return "article/add";
	}
	
	/**
	 * 跳转到添加页面
	 * 
	 * @param model
	 * @return
	 */
	@GetMapping("getCategory")
	public @ResponseBody List<ArticleCategory> getCatecory(Long parentId) {
		Assert.notNull(parentId, "Id can`t be null"); 
		List<ArticleCategory> parent =  articleCategoryService.findByParentId(parentId);
		
		return parent;
	}
	/**
	 * 保存对象
	 */
	@PostMapping("add")
	public @ResponseBody Article save(Article article, @SessionAttribute(Constant.USER_IN_SESSION_KEY) SysAccountInSession sysAccountInSession) {
		SysAccount sysAccount = sysAccountService.findOne(sysAccountInSession.getId());
		article.setSysAccount(sysAccount);
		article.setAddSource(AddSource.后台);
		article.setAuditing(Auditing.通过);
		return articleService.save(article);
	}

	/**
	 * 跳转到更新页面
	 */
	@GetMapping({ "updatePage", "detail" })
	public String update(Long id, Model model) {
		Article article = articleService.findOne(id);
		model.addAttribute("entity", article);
		
		Label[] labelEnum = Label.values();
		model.addAttribute("label", labelEnum);
		
		List<ArticleCategory> oneCategory = articleCategoryService.findByParentId(null);
		model.addAttribute("oneCategory", oneCategory);
		
		return "article/update";
	}

	/**
	 * 更新
	 */
	@PostMapping("update")
	public @ResponseBody Article update(Article article) {
		Assert.notNull(article.getId(), "Id can`t be null");
		Article curArticle = articleService.findOne(article.getId());
		article.setAddSource(curArticle.getAddSource());
		article.setAuditing(curArticle.getAuditing());
		
		article.setUser(curArticle.getUser());
		article.setSysAccount(curArticle.getSysAccount());
		article.setShowNumber(curArticle.getShowNumber());
		article.setClickNumber(curArticle.getClickNumber());
		article.setShareNumber(curArticle.getShareNumber());
		
		return articleService.save(article);
	}

	/**
	 * 删除
	 */
	@PostMapping("delete")
	public @ResponseBody Map<String, String> delete(Article article) {
		Assert.notNull(article.getId(), "Id can`t be null");
		Map<String, String> map = new HashMap<>();
		try {
			articleService.delete(article.getId());
		} catch (Exception e) {
			map.put("Type", "500");
			return map;
		}
		map.put("Type", "200");
		return map;
	}
	
	/**
	 * 置顶
	 */
	@PostMapping("sort")
	public @ResponseBody Map<String, String> sort(Long id) {
		Assert.notNull(id, "Id can`t be null");
		Map<String, String> map = new HashMap<>();
		try {
			articleService.setToTop(id);
		} catch (Exception e) {
			map.put("Type", "500");
			return map;
		}
		map.put("Type", "200");
		return map;
	}
	
	/**
	 * 判断id是否存在
	 * 
	 * @param articleId
	 *           文章ID 
	 * @return 是否存在
	 */
	@RequestMapping("idExists")
	public @ResponseBody String idExists(Long articleId) {
		return articleService.idExists(articleId) ? "" : "账号不存在";
	}
}
